#include "clock.h"

#include <SDL.h>
#include <iostream>

using std::cout; using std::endl;

Clock& Clock::getInstance() {
	static Clock clock; 
	return clock;
}

Clock::Clock() :
	started(false), 
	paused(false), 
	frames(0), 
	timeAtStart(0), timeAtPause(0),
	currTicks(0), prevTicks(0), ticks(0) 
{
	start();
}

Clock::Clock(const Clock& c) :
	started(c.started), 
	paused(c.paused), frames(c.frames), 
	timeAtStart(c.timeAtStart), timeAtPause(c.timeAtPause),
	currTicks(c.currTicks), prevTicks(c.prevTicks), ticks(c.ticks) 
{
	start();
}

void Clock::debug( )
{ 
	cout << "The clock is:" << endl;
	cout << "\tstarted:" << started << endl;
	cout << "\tpaused:" << paused << endl;
	cout << "\tframes:" << frames << endl;
	cout << "\ttimeAtStart:" << timeAtStart << endl;
	cout << "\ttimeAtPause:" << timeAtPause << endl;
	cout << "\tcurrTicks:" << currTicks << endl;
	cout << "\tprevTicks:" << prevTicks << endl;
	cout << "\tticks:" << ticks << endl;
	cout << endl;
}

unsigned Clock::getTicks() const
{
	if (paused) return timeAtPause;
	else return SDL_GetTicks() - timeAtStart; 
}

unsigned Clock::getElapsedTicks()
{ 
	if (paused) return 0;
	currTicks = getTicks();
	ticks = currTicks-prevTicks;
	prevTicks = currTicks;
	return ticks;
}

int Clock::getFps() const
{
	static unsigned lastSecond = getSeconds();
	static unsigned lastFrame = frames;
	static unsigned fps = 0;

	if(getSeconds() > lastSecond) {
		fps = (frames-lastFrame)/(getSeconds()-lastSecond);
		lastSecond = getSeconds();
		lastFrame = frames;
	}
	return fps;
}

Clock& Clock::operator++()
{
	if ( !paused ) ++frames; 
	return *this;
}

Clock  Clock::operator++(int)
{ 
	if ( !paused ) frames++; 
	return *this;
}

void Clock::start()
{ 
	started = true; 
	paused = false; 
	frames = 0;
	timeAtPause = timeAtStart = SDL_GetTicks(); 
}

void Clock::pause()
{
	if( started && !paused ) {
		timeAtPause = SDL_GetTicks() - timeAtStart;
		paused = true;
	}
}

void Clock::unpause()
{
	if( started && paused ) {
		timeAtStart = SDL_GetTicks() - timeAtPause;
		paused = false;
	}
}

